package foo.bar.wiki.web.action.page;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.action.ExtendedPaginatedList;
import foo.bar.wiki.web.action.PaginatedBaseActionBean;
import foo.bar.wiki.web.extension.PageTypeConverter;
import foo.bar.wiki.domain.Page;
import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.plugins.PluginException;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.integration.spring.SpringBean;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class EditPageActionBean extends PaginatedBaseActionBean {


    public static final String VIEW = "/page/editPage.jsp";


    private WikiService wikiService;

    @Validate(converter = PageTypeConverter.class)
    @ValidateNestedProperties({
        @Validate(field = "description", trim = true, required = true, on={"save"}),
        @Validate(field="pageLocation", trim = true, required = true, on={"save"})
    })
    private Page currentPage;

    
    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution(generateThemePath(VIEW));
    }


    public Resolution save() throws PluginException {
        wikiService.updatePage(currentPage);
        getContext().getMessages().add(new SimpleMessage("Page ["+currentPage.getName()+"] saved successfully"));
        return new RedirectResolution(EditPageActionBean.class, "view").addParameter("currentPage", currentPage.getId());
    }



    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }

    public Page getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Page page) {
        this.currentPage = page;
    }
    
}
